package com.amithkoujalgi.framework.rest.util;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.IOUtils;

public class FileUtils
{
	public static List<File> listFilesRecursively( String directoryPath ) throws FileNotFoundException
	{
		if( !new File( directoryPath ).exists() )
		{
			throw new FileNotFoundException( directoryPath + " doesn't exist." );
		}
		if( !new File( directoryPath ).isDirectory() )
		{
			throw new FileNotFoundException( directoryPath + " isn't a directory." );
		}
		return new Filewalker().listFiles( directoryPath );
	}

	public static List<File> listDirectoriesRecursively( String directoryPath ) throws FileNotFoundException
	{
		if( !new File( directoryPath ).exists() )
		{
			throw new FileNotFoundException( directoryPath + " doesn't exist." );
		}
		if( !new File( directoryPath ).isDirectory() )
		{
			throw new FileNotFoundException( directoryPath + " isn't a directory." );
		}
		return new Filewalker().listDirectories( directoryPath );
	}

	public static String read( File f ) throws IOException
	{
		return org.apache.commons.io.FileUtils.readFileToString( f );
	}

	public void writeString( File f, String content ) throws IOException
	{
		FileOutputStream fos = new FileOutputStream( f );
		IOUtils.write( content, fos );
		fos.close();
	}

	public static void moveFileToDir( String srcFile, String destDir ) throws IOException
	{
		File src = new File( srcFile );
		File destDirectory = new File( destDir );
		if( !destDirectory.exists() )
		{
			destDirectory.mkdirs();
		}
		File dest = new File( destDir + File.separator + src.getName() );
		// if (dest.exists()) {
		// dest.delete();
		// }
		src.renameTo( dest );
	}

	public static String cleanFileName( String uncleanFilename )
	{
		uncleanFilename = uncleanFilename.replaceAll( "\\[", "" );
		uncleanFilename = uncleanFilename.replaceAll( "\\]", "" );
		uncleanFilename = uncleanFilename.replaceAll( "\\s", "" );
		uncleanFilename = uncleanFilename.replaceAll( "\\\\", "" );
		uncleanFilename = uncleanFilename.replaceAll( "/", "" );
		uncleanFilename = uncleanFilename.replaceAll( "\\/", "" );
		return uncleanFilename;
	}

	public static boolean copyDirectory( String srcDir, String targetDir ) throws Exception
	{
		try
		{
			org.apache.commons.io.FileUtils.copyDirectory( new File( srcDir ), new File( targetDir ) );
		}
		catch( Exception e )
		{
			throw new Exception( "Couldn't copy index directory '" + srcDir + "' to '" + targetDir + "'." );
		}
		return true;
	}

	public static String sanitizeFileName( String uncleanfileName )
	{
		String dotted = uncleanfileName.replaceAll( "\\.", "__DOT__" );
		String sanitized = dotted.replaceAll( "\\W+", "_" );
		return sanitized.replaceAll( "__DOT__", "." );
	}

	public static String readFile( String filePath ) throws Exception
	{
		@SuppressWarnings("unchecked")
		List<String> lines = IOUtils.readLines( new FileReader( new File( filePath ) ) );
		StringBuffer sb = new StringBuffer();
		for( String s : lines )
		{
			sb.append( s );
			sb.append( "\n" );
		}
		return sb.toString();
	}

	public static void writeFile( String filePath, String contents ) throws Exception
	{
		FileWriter fw = new FileWriter( new File( filePath ) );
		fw.write( contents );
		fw.close();
	}

	public static void appendToFile( String filePath, String contents ) throws Exception
	{
		FileWriter fw = new FileWriter( new File( filePath ), true );
		fw.append( contents + "\n" );
		fw.close();
	}
}

class Filewalker
{
	private List<File> files = new ArrayList<File>();
	private List<File> directories = new ArrayList<File>();

	public List<File> listFiles( String path )
	{
		File root = new File( path );
		File[] list = root.listFiles();
		if( list == null )
			return new ArrayList<File>();
		for( File f : list )
		{
			if( f.isDirectory() )
			{
				directories.add( f );
				listFiles( f.getAbsolutePath() );
			}
			else
			{
				files.add( f );
			}
		}
		return files;
	}

	public List<File> listDirectories( String path )
	{
		File root = new File( path );
		File[] list = root.listFiles();
		if( list == null )
			return new ArrayList<File>();
		for( File f : list )
		{
			if( f.isDirectory() )
			{
				directories.add( f );
				listFiles( f.getAbsolutePath() );
			}
			else
			{
				files.add( f );
			}
		}
		return directories;
	}
}